
xajax.callback.global.onRequest = function()
{
	xajax. $('loadingMsg').style.display = 'block';
};
xajax.callback.global.onComplete = function()
{
	xajax. $('loadingMsg').style.display = 'none';
};

xajax.callback.global.onFailure = function(oRequest)
{
	CMS.debugMessage(oRequest.request.responseText);
};


var CMS = {};


CMS.keybuffer = false;

CMS.onkeyup = function(value,callback) 
{
	CMS.keybuffer=value;
	setTimeout(function() {
		CMS.onkeyup_fire(value,callback);
	},1000);
}

CMS.onkeyup_fire = function(value,callback) {
	if (value == CMS.keybuffer) callback(value);
}

CMS.number_format = function (value) {
	
	return Math.round(value*100000)/100000;
	
	
}


CMS.price_change = function(bruttopricefieldname, nettopricefieldname,taxfieldname,target) {
	
	nettopricefield = document.getElementsByName(nettopricefieldname)[0];
	bruttopricefield = document.getElementsByName(bruttopricefieldname)[0];
	taxfield = document.getElementsByName(taxfieldname)[0];
	
	if ('select-multiple' == taxfield.type || 'select' == taxfield.type) {
		mwst = taxfield.options[taxfield.selectedIndex].value / 100;
	} else {
		mwst = taxfield.value / 100;
	}
	if ('netto' == target) {
		value = bruttopricefield.value;
		value = value.replace(",",".");
		nettopricefield.value = CMS.number_format(value / (1+mwst));
	} else {
		value = nettopricefield.value;
		value = value.replace(",",".");
		bruttopricefield.value = CMS.number_format(value * (1+mwst));
	}
	
}


CMS.debugObject = function (obj,childs) {
	
	for (a in obj) {
		if (childs) {
			CMS.debugMessage(a+": "+obj[a]);
		} else {
			CMS.debugMessage(a);
					
		}
	}
}

CMS.debugWin = null;

CMS.debugMessage = function (message)
{
    
    if (null == xajax.$('CMS_DebugWin'))
    {
        CMS.debugWin = $('<div/>').attr({id:'CMS_DebugWin',className:'ccdsDialog'}).xWindow({title:'Debug', closable:'destroy',width:400,height: 300});
    }
		xajax.$('CMS_DebugWin').innerHTML += message + "<br />";

}



CMS.CheckAllBoxes = function (formid)
{

    for (var i = 0; i < xajax. $(formid).elements.length; i++)
    {
        if (xajax. $(formid).elements[i].type == 'checkbox')
        {
            xajax. $(formid).elements[i].checked = !(xajax. $(formid)
                .elements[i].checked);
        }
    }
}

// - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - - 

CMS.clear_tiny_instances = function()
{
	for (var n in tinyMCE.editors)
	{
		tinyMCE.execCommand('mceRemoveControl', false, tinyMCE.editors[n].editorId);
	}
}



CMS.ListView = function (obj_id,add_params) {
	if (noOverLoad==true) {
		params = new Object;
		params['obj_id']=obj_id;					
		params['parameter']=add_params;					
		xajax_ContentEditorListResults(params);
	}				
}


// - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - - 

CMS.reset_image = function(fieldname)
{
    xajax. $('file' + fieldname).value = '';
    xajax. $('dir' + fieldname).value = '';
    xajax. $('description' + fieldname).value = '';
    xajax. $('label' + fieldname).innerHTML = 'kein Bild gew&auml;hlt';
    xajax. $('img' + fieldname).style.background= 'url(/ccds/img/image-missing.gif) top left no-repeat';
    xajax. $('img' + fieldname).width = '75';
    xajax. $('img' + fieldname).height = '74';
}
//
//// - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - - 
//
CMS.reset_file = function(fieldname)
{
    xajax. $('file' + fieldname).value = '';
    xajax. $('dir' + fieldname).value = '';
    xajax. $('label' + fieldname).innerHTML = 'keine Datei gew&auml;hlt';
}

// - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - -  - - - - 

CMS.LogoutConfirm = function () {
try {	
	$('<div />').xWindow('confirm',{title:"logout",content:"Wollen Sie sich wirklich ausloggen?"}, function(){
	  xajax.request(
	  {
	  		ccdsclass:'ccdsDefaultPlugin',
	      ccdsfun: 'logout'
	  }
	  , 
	  {
	      parameters: []
	  }
	  ); return true; 		
	});
	
} catch (ex) {
	this.debugObject(ex,true);
}
}


CMS.TrashConfirm = function (obj_id,content) {
	
	$('<div />').xWindow('confirm',{title:"Papierkorb",content:content}, function(){
	  xajax.request(
	  {
	  		ccdsclass:'ccdsTrash',
	      ccdsfun: 'TrashDelete'
	  }
	  , 
	  {
	      parameters: [{obj_id:obj_id}]
	  }
	  ); return true; 		
	});
}


CMS.DirSelector = {};
CMS.DirSelector.selected = '';

CMS.FileManager = {};


CMS.FileManager.open = function (config) {
	
		if ('undefined' == typeof config.returnfield) {
			alert("no returnfield defined");
			return;
		}
		
		if ('undefined' == typeof config.post_params) config.post_params={};
		
		this.post_params = config.post_params;
		this.returnfield = config.returnfield;		
		
		this.post_params.root = config.root;
		this.post_params.project_id = config.project_id;
		this.post_params.multiple = 'undefined' != typeof config.multiple ? config.multiple : false;
		
//		this.post_params.return_callback = 'undefined' != typeof config.return_callback ? config.return_callback : 'CMS.FileManager.returnfiles'
		
		if ('undefined' != typeof config.callback) {
			this.callback = config.callback;
		}
    xajax.request(
    {
        ccdsfun: 'FileManagerOpen', ccdsclass: 'ccdsFileManager',ccdscomet:1
    }
    , 
    {
        parameters: [this.post_params],
        comet:1
    }
    );	
	
}

CMS.FileManager.returnfiles = function(values) 
{
		this.post_params.files = values;
		if ('object' == typeof this.callback) {
			xajax.request(
				this.callback,
				{
					parameters:[this.post_params]
					,
					context:this.returnfield	
				}	
			)
		} else if ('function' == typeof this.callback) {
			this.callback(this.post_params);
		} else {
			this.returnfield.file.value = values[0].name;
			this.returnfield.dir.value=values[0].path;
			this.returnfield.label.innerHTML=values[0].name;
			$('#filemanager').xWindow('destroy');
		}

	
}

CMS.AlertWindow = function(msg)  {
	
	$('<div/>').xWindow(
				"alert",	
				{
					title: "Achtung",
					content: msg
				}
		);

	
	
}


//function ChangeGroupPermissions(group_id, right, checked)
//{
//    var inputFields = document.getElementsByTagName("input");
//    for (var inputIndex = 0; inputIndex < inputFields.length; inputIndex++)
//    {
//        if (inputFields[inputIndex].getAttribute("type") == "checkbox")
//        {
//
//            regex = new RegExp("group_" + group_id + "_user_([0-9]?)_" + right);
//            field_id = inputFields[inputIndex].getAttribute("id");
//            if (regex.test(inputFields[inputIndex].getAttribute("id")))
//            {
//                document.getElementById(field_id).checked = checked;
//            }
//        }
//    }
//}
//
//
//function UserAdministrationChangeUserPermissions(obj_id, right, checked)
//{
//    var inputFields = document.getElementsByTagName("input");
//    for (var inputIndex = 0; inputIndex < inputFields.length; inputIndex++)
//    {
//        if (inputFields[inputIndex].getAttribute("type") == "checkbox")
//        {
//            regex = new RegExp(obj_id + "_(.*?)_" + right);
//            field_id = inputFields[inputIndex].getAttribute("id");
//            if (regex.test(inputFields[inputIndex].getAttribute("id")))
//            {
//                document.getElementById(field_id).checked = checked;
//            }
//        }
//    }
//}
//

CMS.ProjectEditSave = function(project_id)
{
    params = new Object;

    if (xajax.$('project_name_'+project_id).value == "")
    {
        CMS.AlertWindow("Sie müssen einen Namen für das Projekt eingeben!");
    }
    else
    {
        xajax.request(
        {
            ccdsclass: 'ccdsProject', ccdsfun: 'ProjectEditSave'
        }
        , 
        {
            parameters: [xajax.getFormValues('project_editform')]
        }
        );
    }
}


CMS.ProjectRootAdd = function()
{
    params = new Object;
    if (document.project_newrootform.obj_name.value == "")
    {
        CMS.AlertWindow("Sie müssen einen Namen für das Objekt eingeben!");
    }
    else
    {
        xajax.request(
        {
            ccdsclass: 'ccdsProject', ccdsfun: 'ProjectRootAdd'
        }
        , 
        {
            parameters: [xajax.getFormValues('project_newrootform')]
        }
        );
    }
}

/* fieldedit list view */
CMS.makeEditable = function(id)
{
	xajax.$(id).onclick = function()
    {
        CMS.editField(xajax.$(id))
    };
    
    xajax.$(id).onmouseover = function()
    {
        CMS.showAsEditable($(id))
    };
    
    xajax.$(id).onmouseout = function()
    {
        CMS.showAsEditable($(id), true)
    };
}

CMS.editField = function(obj)
{
    $(obj).toggle()

    var textarea = '<div id="' + obj.id + '_editor"><input type="text" id="' + obj.id + '_edit" name="' + obj.id + '" value="' + obj.innerHTML + '" class="input_text_small" style="width:40px"/>';
    var button = ' <a href="#" id="' + obj.id + '_save"  >+</a> / <a href="#" id="' + obj.id + '_cancel"  >x</a></div>';

    $(obj).after(textarea + button);

    xajax.$(obj.id+'_save').onclick = function()
    {
        CMS.saveChanges(obj);
    };

    xajax.$(obj.id + '_cancel').onclick = function()
    {
        CMS.cleanUp(obj);
    };
}

CMS.showAsEditable = function(obj, clear)
{
    if (!clear)
    {
        $(obj).addClass('editable');
    }
    else
    {
        $(obj).removeClass('editable');
    }
}

CMS.saveChanges = function(obj)
{
    var new_content = escape($('#'+obj.id + '_edit').val());
    $(obj).html = "Saving...";
    CMS.cleanUp(obj, true);

    var success = function(t)
    {
        CMS.editComplete(t, obj);
    };
    var failure = function(t)
    {
        CMS.editFailed(t, obj);
    };
    var url = 'edit.php';
    var pars = 'id=' + obj.id + '&content=' + new_content;
    
    xajax.request(
        {
            ccdsclass: 'ccdsContentEditor', ccdsfun: 'ContentEditorSerializeSortable'
        }
        , 
        {
            parameters: [{obj_id:obj.id, position:new_content}]
        }
    );
}

CMS.cleanUp = function(obj, keepEditable)
{
    $('#'+obj.id+'_editor').remove();
    $(obj).toggle();
    if (!keepEditable)
        CMS.showAsEditable(obj, true);
}

CMS.editComplete = function(responseText, obj)
{
    $(obj).html = responseText;
    CMS.showAsEditable(obj, true);
}

CMS.editFailed = function(t, obj)
{
    $(obj).html = 'Sorry, the update failed.';
    CMS.cleanUp(obj);
}
/* fieldedit list view */


CMS.hide_timeout = null;

CMS.hide_navi = function () {
	
	CMS.hide_timeout = window.setTimeout(CMS.hide_navi_timeout, 1000);
}

CMS.hide_navi_timeout = function () {

	xajax.$('CMS_NaviTree_Box').style.left='-300px';
}

CMS.show_navi = function () {
	if (null != CMS.hide_timeout) window.clearTimeout(CMS.hide_timeout);
	CMS.hide_timeout= null;
	xajax.$('CMS_NaviTree_Box').style.left='0px';
}

